package com.ccrego.haiyi.quickysee;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.Toast;

import com.ccrego.haiyi.R;
import com.ccrego.haiyi.account.AccountManager;
import com.ccrego.haiyi.account.UrlCenter;
import com.ccrego.haiyi.app.bean.MessageDetailBean;
import com.ccrego.haiyi.common.http.MyHttpRequest;
import com.pachong.android.baseuicomponent.fragment.BaseFragment;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;

import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * Created by Andy Hong on 10/20/2017.
 */

public class MessageDetailFragment extends BaseFragment {
    private WebView mWebView;
    private MyHttpRequest httpRequest;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_message_detail, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mWebView = findView(R.id.mWebView);
        long id = getArguments().getLong("id");
        httpRequest = new MyHttpRequest(getContext());
        Map<String, String> map = new HashMap<>();
        map.put("Custom", AccountManager.getTokenInfo().getUser_id());
        httpRequest.get(String.format(UrlCenter.ARTICLE_URL, id),
                null, map, new DataRequestListener<MessageDetailBean>(MessageDetailBean.class) {
                    @Override
                    public void onStart(Context context, String url) {
                        super.onStart(context, url);
                    }

                    @Override
                    public void onSuccess(MessageDetailBean s) {
                        hideProgress();
                        mWebView.loadData(s.getContent(), "text/html; charset=UTF-8", null);
                    }

                    @Override
                    public void onFailure(Throwable t, int errorNo, String strMsg) {
                        Toast.makeText(getContext(), strMsg, Toast.LENGTH_SHORT).show();
                        hideProgress();
                    }
                });

    }


}
